package com.activenetwork.smlite.testcase.events.searchEvents;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.schema.testcase.events.searchevents.searchevents004.SearchEvents004DataType;
import com.activenetwork.smlite.business.EventBusiness;
import com.activenetwork.smlite.page.generic.ModelPage;
import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.PageLoader;
import com.activenetwork.util.jaxb.ScriptDataBindHelper;
import com.activenetwork.util.verification.gson.FailurePoint;

public class SearchEvents004 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(SearchEvents004.class);
	private SearchEvents004DataType data;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup(){
		try{
			ScriptDataBindHelper helper = new ScriptDataBindHelper(SearchEvents004.class);
			data = helper.bind(SearchEvents004DataType.class);
		}catch(IOException e){
			throw new RuntimeException("Unable to bind the script data for " + this.getClass().getName());
		}
	}
	
	@Test
	public void  Search_Event_By_Tag(){		
		loginBusiness.login();
		
		// Delete the event with specific tag
		EventBusiness eventBusiness = new EventBusiness();
		try{
			eventBusiness.deleteEvent(data.getEvent().getBasic().getTitle(), data.getEvent());
		}catch(RuntimeException e){
			logger.info("No need to delete the event since it is not existing");
		}
		
		// Create an event with specific tag
		eventBusiness.addEvent(data.getEvent());
		
		// Try to search this location in lower case
		PageLoader.load(ModelPage.class).events();
		String tagName = data.getEvent().getBasic().getTag().get(0).getTagName();
		try{
			eventBusiness.searchEvent(tagName, data.getEvent());
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(SearchEvents004.class.getName(), 
					"Verify search by tag", 
					"Search specific tag can not find the event"));
		}
		
		// Try to search this location in upper case
		PageLoader.load(ModelPage.class).events();
		try{
			eventBusiness.searchEvent(tagName.toUpperCase(), data.getEvent());
		}catch(RuntimeException e){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(SearchEvents004.class.getName(), 
					"Verify search by tag", 
					"Search specific tag can not find the event with upper case"));
		}
		
		loginBusiness.logout();
	}

}
